package com.csnf.demo7.Student;

import com.csnf.demo7.dao.Impl.TbDourseDaoImpl;
import com.csnf.demo7.dao.TbCourseDao;
import com.csnf.demo7.entity.TbCourse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@WebServlet({"/tbStudent", "/tbById", "/tbStudentById"})
public class TbCourseStudent extends HttpServlet {
    TbCourseDao dao = new TbDourseDaoImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException, ServletException {
        String method = request.getServletPath();
        switch (method) {
            case "/tbById":
                dao.deleteCourse(Integer.parseInt(request.getParameter("id")));
                request.setAttribute("courseList", dao.getCourseList());
                break;
            case "/tbStudent":
                String courseName = request.getParameter("courseName");
                if (courseName != null && !courseName.isEmpty()) {
                    request.setAttribute("courseList", dao.getCourseListBname(courseName));
                } else {
                    request.setAttribute("courseList", dao.getCourseList());
                }

                break;
            case "/tbStudentById":
                request.setAttribute("all", dao.getCourseById(Integer.parseInt(request.getParameter("id"))));
                request.setAttribute("courseList", dao.getCourseList());
                request.getRequestDispatcher("bianji.jsp").forward(request, response);
                break;
        }
        request.getRequestDispatcher("tb_course.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        Integer count = Integer.parseInt(request.getParameter("count"));
        LocalDate startDate = LocalDate.parse(request.getParameter("startDate"));
        LocalDate endDate = LocalDate.parse(request.getParameter("endDate"));
        String teacher = request.getParameter("teacher");
        String method = request.getServletPath();
        switch (method) {
            case "/tbStudentById":
                dao.addCourse(new TbCourse(null, name, count, startDate, endDate, teacher));
                break;
            case "/tbStudent":
                String id = request.getParameter("id");
                dao.updateCourse(new TbCourse(Integer.parseInt(id), name, count, startDate, endDate, teacher));
                request.setAttribute("courseList", dao.getCourseList());
                request.getRequestDispatcher("tb_course.jsp").forward(request, response);
                break;
        }
        request.setAttribute("courseList", dao.getCourseList());
        request.getRequestDispatcher("tianjia.jsp").forward(request, response);

    }
}


